package com.personal.kousuan;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.personal.kousuan.domain.Question;
import com.personal.kousuan.generator.questionlist.GeneralKouSuanQuestionGenerator;

public class KouSuanQuestionListFragment extends Fragment {

	private ListView questionListView;
	private Button backbtn;
	private CheckBox showAnswerCheckbox;
	SimpleAdapter adapter;
	List<Question> questionList;
	List<Map<String,String>> mapList = new ArrayList<Map<String,String>>();
	
	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {  
            int count = 0;  
            Bundle bundle = msg.getData();
            boolean isChecked = bundle.getBoolean("isChecked");
            if(!isChecked){
            	for(Map<String,String> map:mapList){
					map.put(Question.answer_key, "");
				}	
				System.out.println(mapList);
				
            }else{
            	mapList.clear();
            	int index = 1;
        		for(Question q:questionList)
        		{
        			q.setId(index);
        			index++;
        			mapList.add(q.getMap());
        		}
            }
            adapter.notifyDataSetChanged();	
			questionListView.setAdapter(adapter);
              
        };  
	};
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		System.out.println("==onCreateView");
		View view = inflater.inflate(R.layout.kousuan_question_list, container, false);
		showAnswerCheckbox = (CheckBox)view.findViewById(R.id.showanswer_checkbox);
		
		questionListView = (ListView)view.findViewById(R.id.questionListView);
		backbtn = (Button)view.findViewById(R.id.questionlist_back);
		
		GlobalSetting globalSetting = (GlobalSetting)getActivity().getApplicationContext();
		System.out.println(globalSetting);
		
		String categoryKey = globalSetting.getCategory();
		
		GeneralKouSuanQuestionGenerator generator = new GeneralKouSuanQuestionGenerator();
		questionList = generator.generateKouSuanQuestions(Integer.parseInt(globalSetting.getNumberRange()),categoryKey);
		
		
		mapList.clear();
		int index = 1;
		for(Question q:questionList)
		{
			q.setId(index);
			index++;
			mapList.add(q.getMap());
		}
		
		adapter = new SimpleAdapter(view.getContext(), mapList, R.layout.kousuan_question_list_item, new String[]{Question.id_key,Question.question_key,Question.answer_key}, new int[] {R.id.item_questionId,R.id.item_question,R.id.item_answer});
		adapter.notifyDataSetChanged();	
		questionListView.setAdapter(adapter);
				
		
		backbtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Fragment  questionListFragment = new HomeFragment();
				FragmentTransaction transaction = getFragmentManager().beginTransaction();
				transaction.replace(R.id.exercise_fragment, questionListFragment);
				
//				transaction.addToBackStack(null);
				transaction.commit();
				
			}
		});
		
		showAnswerCheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				System.out.println("isChecked:"+isChecked);
				Message message = new Message();  
	            Bundle bundle = new Bundle();  
	            bundle.putBoolean("isChecked", isChecked);  
	            message.setData(bundle);  
	            handler.sendMessage(message);  
			}
		});
		
		return view;
	}
}
